"use strict";

angular.module('app.sensor').controller('DataChartsCtrl', function ($scope, $stateParams, $http, $cookieStore, $location) {
    $scope.deviceid = $location.search().deviceid;
    $scope.userid = $cookieStore.get('userid');
    $scope.fid = $cookieStore.get('userinfo').fid;
    $scope.paginationConf = {
        currentPage: 1,
        itemsPerPage: 10,
        pagesLength: 5,
        totalItems: 20,
        perPageOptions: [10, 20, 30, 40, 50],
        plan: 0,
        onChange: function () {
        }
    };
    //折线图
    var myChart1 = echarts.init(document.getElementById('line'));
    var option1 = {
        title: {
            text: ''
        },
        tooltip: {
            trigger: 'axis'
        },
        legend: {
            data: ['邮件营销', '联盟广告', '视频广告', '直接访问', '搜索引擎']
        },
        grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
        },
        xAxis: {
            type: 'category',
            boundaryGap: false,
            data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
        },
        yAxis: {
            type: 'value'
        },
        series: [
            {
                name: '邮件营销',
                type: 'line',
                stack: '总量',
                data: [120, 132, 101, 134, 90, 230, 210]
            },
            {
                name: '联盟广告',
                type: 'line',
                stack: '总量',
                data: [220, 182, 191, 234, 290, 330, 310]
            },
            {
                name: '视频广告',
                type: 'line',
                stack: '总量',
                data: [150, 232, 201, 154, 190, 330, 410]
            },
            {
                name: '直接访问',
                type: 'line',
                stack: '总量',
                data: [320, 332, 301, 334, 390, 330, 320]
            },
            {
                name: '搜索引擎',
                type: 'line',
                stack: '总量',
                data: [820, 932, 901, 934, 1290, 1330, 1320]
            }
        ]
    };

    // 1end
    // 柱状图
    var myChart2 = echarts.init(document.getElementById('bar'));
    var option2 = {
        tooltip: {
            trigger: 'axis',
            axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
            }
        },
        legend: {
            data: ['直接访问', '邮件营销', '联盟广告', '视频广告', '搜索引擎']
        },
        grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
        },
        xAxis: {
            type: 'value'
        },
        yAxis: {
            type: 'category',
            data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
        },
        series: [
            {
                name: '直接访问',
                type: 'bar',
                stack: '总量',
                label: {
                    normal: {
                        show: true,
                        position: 'insideRight'
                    }
                },
                data: [320, 302, 301, 334, 390, 330, 320]
            },
            {
                name: '邮件营销',
                type: 'bar',
                stack: '总量',
                label: {
                    normal: {
                        show: true,
                        position: 'insideRight'
                    }
                },
                data: [120, 132, 101, 134, 90, 230, 210]
            },
            {
                name: '联盟广告',
                type: 'bar',
                stack: '总量',
                label: {
                    normal: {
                        show: true,
                        position: 'insideRight'
                    }
                },
                data: [220, 182, 191, 234, 290, 330, 310]
            },
            {
                name: '视频广告',
                type: 'bar',
                stack: '总量',
                label: {
                    normal: {
                        show: true,
                        position: 'insideRight'
                    }
                },
                data: [150, 212, 201, 154, 190, 330, 410]
            },
            {
                name: '搜索引擎',
                type: 'bar',
                stack: '总量',
                label: {
                    normal: {
                        show: true,
                        position: 'insideRight'
                    }
                },
                data: [820, 832, 901, 934, 1290, 1330, 1320]
            }
        ]
    };
    //2end
    //饼图
    var myChart3 = echarts.init(document.getElementById('pie'));
    var option3 = {
        title: {
            text: '',
            subtext: '',
            x: 'center'
        },
        tooltip: {
            trigger: 'item',
            formatter: "{a} <br/>{b} : {c} ({d}%)"
        },
        legend: {
            orient: 'vertical',
            left: 'left',
            data: ['直接访问', '邮件营销', '联盟广告', '视频广告', '搜索引擎']
        },
        series: [
            {
                name: '访问来源',
                type: 'pie',
                radius: '55%',
                center: ['50%', '60%'],
                data: [
                    { value: 335, name: '直接访问' },
                    { value: 310, name: '邮件营销' },
                    { value: 234, name: '联盟广告' },
                    { value: 135, name: '视频广告' },
                    { value: 1548, name: '搜索引擎' }
                ],
                itemStyle: {
                    emphasis: {
                        shadowBlur: 10,
                        shadowOffsetX: 0,
                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                    }
                }
            }
        ]
    };
    myChart1.setOption(option1);
    myChart2.setOption(option2);
    myChart3.setOption(option3);
    //3end
    $http.get(baseurl + "/protocol_field_get").then(function (res) {
        //console.log(res.data);
        if (res.data.ret == 1) {
            $scope.fields = res.data.info;
        }
    });
    //刷新列表
    $scope.sends = function () {
        $scope.page_obj = {
            page: $scope.paginationConf.currentPage,
            rows: $scope.paginationConf.itemsPerPage
        };
        // 获得图表数据
        $http.post(baseurl + '/list/select/charts', $scope.page_obj).then(function (res) {
            console.log(res.data);
            if (res.data.ret == 1 && res.data.rows.length > 0) {
                $scope.types = res.data.rows;
                $scope.paginationConf.totalItems = res.data.records;
            }
        });
    };
    // 删除这条预算成本数据
    $scope.showDelDialog = function (id) {
        $scope.dele_id = id;
    };
    $scope.sure_del = function () {
        $http.delete(baseurl + '/data/charts?id==' + $scope.dele_id).then(function (res) {
            //console.log(res.data);
            if (res.data.ret == 1) {
                $("#delAlert").modal('hide');
                new Notify('success', '删除成功');
                $scope.sends();
            }
        })
    };
    $scope.jump_chart = function () {
        window.location.href = '#/watchcharts';
    }
    $scope.$watch('paginationConf.currentPage + paginationConf.itemsPerPage', $scope.sends);
    //清除图表
    function clear() {
        if (myChart1 != null) {
            myChart1.clear();
        }
        if (myChart2 != null) {
            myChart2.clear();
        }
        if (myChart3 != null) {
            myChart3.clear();
        }
    }
    //增加方案
    $scope.show_addbox = function () {
        $scope.addobj = {};
        $scope.editdata = {};
    }
    //增加方案到数据库
    $scope.editdata = {};
    $scope.addPlan = function (id) {
        $scope.sumobj = {};       
        $scope.sumobj.field = "";
        $scope.sumobj.type = "";
        $scope.sumobj.name = $scope.addobj.name;
        if ($scope.addobj.name == '' || $scope.addobj.name == null || $scope.addobj.name == undefined) {
            new Notify('info', '方案名称不能为空');
            return;
        }
        if ($scope.editdata.line == undefined && $scope.editdata.bar == undefined && $scope.editdata.pie == undefined) {
            new Notify('info', '请至少选择一个图表类型和字段');
            return;
        }
        if ($scope.editdata.line != '' && $scope.editdata.line != null && $scope.editdata.line != undefined) {
            if ($scope.sumobj.field == '') {
                $scope.sumobj.field += $scope.editdata.line;
                $scope.sumobj.type += 'line';
            } else {
                $scope.sumobj.field += "," + $scope.editdata.line;
                $scope.sumobj.type += ","+ 'line';
            }
        } else {
            $scope.sumobj.field += '';
            $scope.sumobj.type += '';
        }
        if ($scope.editdata.bar != '' && $scope.editdata.bar != null && $scope.editdata.bar != undefined) {
            if ($scope.sumobj.field == '') {
                $scope.sumobj.field += $scope.editdata.bar;
                $scope.sumobj.type += 'bar';
            } else {
                $scope.sumobj.field += "," + $scope.editdata.bar;
                $scope.sumobj.type += "," + 'bar';
            }            
        } else {
            $scope.sumobj.field += '';
            $scope.sumobj.type += '';
        }
        if ($scope.editdata.pie != '' && $scope.editdata.pie != null && $scope.editdata.pie != undefined) {
            if ($scope.sumobj.field == '') {
                $scope.sumobj.field += $scope.editdata.pie;
                $scope.sumobj.type += 'pie';
            } else {
                $scope.sumobj.field += "," + $scope.editdata.pie;
                $scope.sumobj.type += "," + 'pie';
            }
        } else {
            $scope.sumobj.field += '';
            $scope.sumobj.type += '';
        }
        $http.post(baseurl + "data/charts", $scope.sumobj).then(function (res) {
            $scope.sends();
            if (res.data.ret == 1) {
                $scope.addobj = {};
                $scope.editdata = {};
            }
        })
    };
    $scope.addobj = {};
    $scope.editobj = {};
    //修改图表
    $scope.Edit_pro = function (obj) {
        $(".bbb").removeClass("bg");
        $(".bbb" + obj.id).addClass("bg");
        $scope.editobj = obj;
        $scope.addobj = $scope.editobj;
        var obj = get_type_field();
        $scope.editdata = obj;
        $("#editModal").modal("show");
        $(".editplan").attr("disabled", false);
    };
    //处理字段和类型的数据
    function get_type_field(xz) {
        var t1 = $scope.addobj.type.split(',');
        var t2 = $scope.addobj.field.split(',');
        if (t1.length != t2.length)
            return null;
        var obj = {};
        for (var i = 0; i < t1.length; i++)
            obj[t1[i]] = t2[i];
        return obj;
    }
    // 确定编辑图表
    $scope.sure_edit = function () {
        var obj = $scope.editdata;
        var objret = {};
        var type_str = "";
        var field_str = "";
        $.each(obj, function (type, field) {
            if (field != "") {
                type_str += type + ",";
                field_str += field + ",";
            }
        });
        type_str = type_str.substr(0, type_str.length - 1);
        field_str = field_str.substr(0, field_str.length - 1);
        objret.id = $scope.addobj.id;
        objret.type = type_str;
        objret.field = field_str;
        objret.name = $scope.addobj.name;
        objret.fid = $scope.addobj.fid;
        $http.put(baseurl + '/data/charts?id==' + $scope.editobj.id, objret).then(function (res) {
            if (res.data.ret == 1) {
                $("#editAlert").modal('hide');
                new Notify('success', '修改成功');
                $scope.sends();
                $(".editplan").attr("disabled", true);
                $scope.addobj = {};
                $scope.editdata = {};
                $(".bbb").removeClass("bg");
            }
        })
    };
})